package dwl.graduation.design.controller;

import dwl.graduation.design.domain.*;
import dwl.graduation.design.service.WorkService;
import dwl.graduation.design.utils.MessageConstant;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import dwl.graduation.design.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/work")
public class WorkController {

    @Autowired
    private WorkService workService;

    @RequestMapping(value = "/start",method = RequestMethod.POST)
    public Result startProcessInstance(@RequestBody StartApplyRequest startApplyRequest){
        ProcessInstanceDTO instance=workService.startProcess(startApplyRequest);
        return new Result(true, MessageConstant.START_APPLY_SUCCESS,instance);
    }

    @RequestMapping(value = "/addFurn",method = RequestMethod.POST)
    public Result addFurniture(@RequestBody InsertFurnitureRequest insertFurnitureRequest) throws ParseException {
        workService.insertFurniture(insertFurnitureRequest);
        return new Result(true,MessageConstant.ADD_FURNITURE_SUCCESS);
    }

    @RequestMapping(value = "/findFurnPage",method = RequestMethod.POST)
    public PageResult findFurniture(@RequestBody QueryPageBean queryPageBean){
        return workService.findPageList(queryPageBean);
    }

    @RequestMapping(value = "/getTask",method = RequestMethod.GET)
    public PageResult queryProcessInstance(@RequestParam("userName") String userName){
        return workService.getTaskList(userName);
    }

    @RequestMapping(value = "/complete",method = RequestMethod.POST)
    public Result approvedTask(@RequestBody TaskRequest taskRequest, HttpServletRequest request){
        return workService.approvedTask(taskRequest);
//        return new Result(true, MessageConstant.APPROVED_TASK_SUCCESS);
    }

    @RequestMapping(value = "/status",method = RequestMethod.GET)
    public Result getProcessStatus(@RequestParam("processInstanceId") String processInstanceId){
        Map<String,Object> ret = workService.getProcessStatus(processInstanceId);
        return new Result(true,MessageConstant.GET_APPROVAL_SUCCESS,ret);
    }

    @RequestMapping(value = "/history",method = RequestMethod.GET)
    public Result getHiTaskByUser(@RequestParam("userName") String userName){
        List<HiTaskResponse> taskResponseList = workService.getHiTaskByUser(userName);
        return new Result(true,MessageConstant.GET_HISTORY_SUCCESS,taskResponseList);
    }

}
